package com.example.myapplication.register;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.okhttp.HttpUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class User extends AppCompatActivity {
    @BindView(R.id.resetpwd_edit_name)
    EditText name;
    @BindView(R.id.resetpwd_edit_pwd_old)
    EditText oldpwd;
    @BindView(R.id.resetpwd_edit_pwd_new)
    EditText newpwd;
    @BindView(R.id.resetpwd_edit_national)
    EditText national;
    @BindView(R.id.resetpwd_edit_address)
    EditText address;
    @BindView(R.id.resetpwd_edit_like_national)
    EditText like_national;
    @BindView(R.id.register_btn_sure)
    Button sure;
    @BindView(R.id.register_btn_cancel)
    Button cancel;
    private String user_name , user_pwd , user_pwd_aga , user_national , like_nation , user_addr;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.me_register);
        ButterKnife.bind(this);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(User.this, "可以点击1", Toast.LENGTH_SHORT).show();
                checkGetResult();
                Toast.makeText(User.this, "可以点击2", Toast.LENGTH_SHORT).show();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void checkGetResult() {
        if (!name.getText().toString().isEmpty()&&
                !oldpwd.getText().toString().isEmpty()&&
                !newpwd.getText().toString().isEmpty()&&
                !national.getText().toString().isEmpty()&&
                !address.getText().toString().isEmpty()&&
                !like_national.getText().toString().isEmpty()){
            user_name = name.getText().toString();
            user_pwd = oldpwd.getText().toString();
            user_pwd_aga = newpwd.getText().toString();
            user_national = national.getText().toString();
            like_nation = like_national.getText().toString();
            user_addr = address.getText().toString();
            if (!user_pwd.equals(user_pwd_aga)){
                Toast.makeText(this, "两次密码不正确哦！", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "可以发送到数据库", Toast.LENGTH_SHORT).show();
                sendToDataBase();
            }
        }else{
            Toast.makeText(this, "您还有未输入的哦", Toast.LENGTH_SHORT).show();
        }
    }

    private void sendToDataBase() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append("users?user_name="+user_name+"&");
                    stringBuilder.append("user_password="+user_pwd+"&");
                    stringBuilder.append("national="+user_national+"&");
                    stringBuilder.append("addr="+user_addr+"&");
                    stringBuilder.append("likeNational="+like_nation+"&");
                    HttpUtil.send(stringBuilder.toString());
                    System.out.println("已发送到数据库");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(User.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });
                }catch (Exception e ){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
